package org.littlesun.management.ui.login;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class LoginDialog extends Dialog {

	private LoginDialogController controller;
	protected boolean result = false;
	Shell shell;
	Combo usernameCombo;
	Text passwordText;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public LoginDialog(Shell parentShell) {
		super(parentShell);
		this.controller = new LoginDialogController();
		this.shell = parentShell;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) container.getLayout();
		gridLayout.numColumns = 4;

		Label usernameLabel = new Label(container, SWT.NONE);
		usernameLabel.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		usernameLabel.setText("\u4F7F\u7528\u8005\u4EE3\u78BC");
		GridData gd_labelPrompt = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_labelPrompt.widthHint = 100;
		usernameLabel.setLayoutData(gd_labelPrompt);

		this.usernameCombo = new Combo(container, SWT.BORDER);
		usernameCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
//				boolean show = true;
//				OS.SendMessage(usernameCombo.handle, OS.CB_SHOWDROPDOWN, show ? 1 : 0, 0);
			}
		});
		this.usernameCombo.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		usernameCombo.setTextLimit(50);
		GridData gd_username = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
		gd_username.widthHint = 300;
		usernameCombo.setLayoutData(gd_username);

		Label passwordLabel = new Label(container, SWT.NONE);
		passwordLabel.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		passwordLabel.setText("\u5BC6\u78BC");
		GridData gd_usernameLabel = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_usernameLabel.widthHint = 100;
		passwordLabel.setLayoutData(gd_usernameLabel);

		passwordText = new Text(container, SWT.BORDER);
		passwordText.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		passwordText.setEchoChar('*');
		passwordText.setTextLimit(30);
		GridData gd_password = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
		gd_password.widthHint = 300;
		passwordText.setLayoutData(gd_password);

		this.controller.bind(this);
		
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	@Override
	protected void okPressed() {
		boolean loginSucc = controller.login();
		if (loginSucc == true) {
			super.okPressed();
		}
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(449, 178);
	}

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		LoginDialog loginForm = new LoginDialog(shell);
		loginForm.open();
	}
	
}
